<?php

use nkostadinov\user\models\Token;
use nkostadinov\user\models\User;
use nkostadinov\user\tests\_pages\PasswordRecoveryRequestPage;
use nkostadinov\user\tests\_pages\PasswordResetPage;

/**
 * Tests the password recovery functionality.
 */
class RecoveryCest
{

    private $mailDir;

    public function _before(FunctionalTester $I)
    {
        $this->mailDir = Yii::getAlias('@runtime/mail');
    }

    public function _after(FunctionalTester $I)
    {
        User::deleteAll('email = :email', [':email' => Commons::TEST_EMAIL]);
        Utils::cleanDir($this->mailDir); // Delete all emails generated by the tests
    }

    public function testPasswordRecoveryRequest(FunctionalTester $I)
    {
        // Create one user
        $user = Commons::createUser();

        $I->expectTo('see the recovery form when going to the recovery page');
        $passwordRecoveryRequestPage = PasswordRecoveryRequestPage::openBy($I);
        $I->seeElement('#password-recovery-form');

        $I->expectTo('see that a proper message is sent when an empty email is set');
        $passwordRecoveryRequestPage->submitRecoveryForm('');
        $I->seeElement('#password-recovery-form');
        $I->see('Email cannot be blank');

        $I->expectTo('see that a proper message is sent when a wrong email is set');
        $passwordRecoveryRequestPage->submitRecoveryForm('test@fds');
        $I->seeElement('#password-recovery-form');
        $I->see('Email is not a valid email address.');

        $I->expectTo('see that a proper message is sent when an email of unexisting user is set');
        $passwordRecoveryRequestPage->submitRecoveryForm('gfds@afgd.bg');
        $I->seeElement('#password-recovery-form');
        $I->see('There is no user with this email address');

        $I->amGoingTo('save the form with correct data');
        $passwordRecoveryRequestPage->submitRecoveryForm(Commons::TEST_EMAIL);

        $I->expectTo('see the user is redirected to the correct page');
        $I->see('Recovery message sent');

        $I->expectTo('see that a the token is saved to the database');
        $I->seeRecord(Token::className(), ['user_id' => $user->id]);

        $I->expectTo('see that a recovery email is sent to the user');
        $I->assertNotEmpty($this->mailDir);
    }

    public function testResetPassword(FunctionalTester $I)
    {
        $user = Commons::createUnconfirmedUser();
        $token = Commons::createTokenForUser($user->id);

        $I->amGoingTo('test that the reset password functionality is working properly');

        $I->amGoingTo('to confirm the email for the user');
        PasswordResetPage::openBy($I, ['code' => $token->code]);

        $I->expectTo('see successful reset');
        $I->dontSeeRecord(Token::className(), ['user_id' => $user->id]);
        
        $I->expectTo('see the user is sent to the change password form');
        $I->see('Change password');
        $I->seeElement('#changepasswordform-newpassword');
        $I->seeElement('#changepasswordform-newpasswordrepeat');
    }
}
